package com.game.web.controller.game;

import com.game.common.annotation.Log;
import com.game.common.config.RuoYiConfig;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.AjaxResult;
import com.game.common.core.page.TableDataInfo;
import com.game.common.enums.BusinessType;
import com.game.common.utils.file.FileUploadUtils;
import com.game.common.utils.file.MimeTypeUtils;
import com.game.common.utils.poi.ExcelUtil;
import com.game.system.domain.game.Game;
import com.game.system.service.IGameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 游戏Controller
 *
 * @author Mr.Liu
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/game/game")
public class GameController extends BaseController
{
    @Autowired
    private IGameService gameService;

    /**
     * 查询游戏分页列表
     */
    @PreAuthorize("@ss.hasPermi('game:game:list')")
    @GetMapping("/page/list")
    public TableDataInfo pageList(Game game)
    {
        startPage();
        List<Game> list = gameService.selectGameList(game);
        return getDataTable(list);
    }

    /**
     * 查询游戏列表
     */
    @PreAuthorize("@ss.hasPermi('game:game:list')")
    @GetMapping("/list")
    public AjaxResult list(Game game) {
        List<Game> list = gameService.selectGameList(game);
        return success(list);
    }


    /**
     * 导出游戏列表
     */
    @PreAuthorize("@ss.hasPermi('game:game:export')")
    @Log(title = "游戏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Game game)
    {
        List<Game> list = gameService.selectGameList(game);
        ExcelUtil<Game> util = new ExcelUtil<Game>(Game.class);
        util.exportExcel(response, list, "游戏数据");
    }

    /**
     * 获取游戏详细信息
     */
    @PreAuthorize("@ss.hasPermi('game:game:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gameService.selectGameById(id));
    }

    /**
     * 新增游戏
     */
    @PreAuthorize("@ss.hasPermi('game:game:add')")
    @Log(title = "游戏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Game game)
    {
        return toAjax(gameService.insertGame(game));
    }

    /**
     * 修改游戏
     */
    @PreAuthorize("@ss.hasPermi('game:game:edit')")
    @Log(title = "游戏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Game game)
    {
        return toAjax(gameService.updateGame(game));
    }

    /**
     * 删除游戏
     */
    @PreAuthorize("@ss.hasPermi('game:game:remove')")
    @Log(title = "游戏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gameService.deleteGameByIds(ids));
    }


    /**
     * icon上传
     */
    @Log(title = "icon上传", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadIcon")
    public AjaxResult avatar(MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", avatar);
            return ajax;
        }
        return error("上传图片异常");
    }
}
